/*
 * Copyright 2010 Pierre Heinzerling, Uwe Janner, Patrick Wright
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.upp.publinksky.util;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.upp.publinksky.dao.LinkCommentDAO;
import com.upp.publinksky.dao.LinkDAO;
import com.upp.publinksky.dao.ThemeDAO;
import com.upp.publinksky.model.Link;
import com.upp.publinksky.model.LinkComment;
import com.upp.publinksky.model.Theme;

import javax.jdo.PersistenceManager;
import java.util.Random;

/**
 */
public class RandomDataGenerator {
    public static final Random random = new Random();

    public static String randomThemeName() {
        return String.format("theme-named-%d", random.nextInt());
    }

    public static String randomEmail() {
        return String.format("user-%d@gmail.com", random.nextInt());
    }
    public static String randomTitle() {
        return String.format("Page Title: %s", random.nextInt());
    }

    public static long randomId() {
        return random.nextLong();
    }
    public static int randomInt(int from, int to) {
        return random.nextInt(to) + from;
    }

    public static String randomUri() {
        return "www.domain" + random.nextInt() + ".com";
    }
    public static String randomNote() {
        return "another comment on this link: " + random.nextInt();
    }

    public static <T> Key randomKey(final Class<T> klass) {
        return KeyFactory.createKey(klass.getName(), randomId());
    }

    public static Link storeNewRandomLink(final Theme theme, final PersistenceManager pm) {
        return LinkDAO.insertLink(theme, randomEmail(), randomTitle(), randomUri(), pm);
    }

    public static Theme storeNewRandomTheme(PersistenceManager apm) {
        return storeNewRandomTheme(randomThemeName(), apm);
    }

    public static Theme storeNewRandomTheme(final String randomName, PersistenceManager pm) {
        return ThemeDAO.insertTheme(randomName, randomEmail(), pm);
    }

    public static LinkComment storeNewRandomLinkComment(PersistenceManager pm) {
        Theme theme = storeNewRandomTheme(pm);
        String fromEmail = randomEmail();
        String uri = randomUri();
        return LinkCommentDAO.insertComment(theme.getKey(), fromEmail, uri, "", theme.getLastUpdateCounter(), pm);
    }
}
